package com.cskaoyan.service;

import com.cskaoyan.dao.OrderDao;
import com.cskaoyan.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * @author stone
 * @date 2021/12/27 15:00
 */
//@Component //userServiceImpl → 组件id的默认值是类名的首字母小写的形式
//@Component(value = "userService")//使用注解的value属性指定
@Component("userService")//如果注解中只有value属性，‘value=’这一部分可以省略掉
//@Scope("prototype") //value属性里直接写作用域就可以了
public class UserServiceImpl implements UserService{

    @Autowired
    @Qualifier("userDaoImpl1") //按照类型和id引入
    UserDao userDao1;
    @Resource(name = "userDaoImpl2") // 使用@Resource注解的name属性
    UserDao userDao2;
    @Autowired   //按照类型
    OrderDao orderDao;

    //@Value("songge") //可以做解耦 → 将这些值使用properties配置文件统一的管理起来
                     //通过key来引入value
    @Value("${username}") //${}引入key → SpEL → Spring Expression Language
    String username;



    @PostConstruct //init-method
    public void myinit() {
        System.out.println("自定义init方法");
    }
    @PreDestroy //destroy-method
    public void mydestroy() {
        System.out.println("自定义destroy方法");
    }

    @Override
    public void serviceMethod() {
        userDao1.method1();
        userDao2.method1();
        orderDao.method2();
        System.out.println(username + ":helloworld");
    }
}
